/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2013-11-27         luwei
 */

package com.tianci.appstore.ui.control;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.framework.utils.LogUtils;
import com.paster.util.JsonUtil;
import com.tianci.appstore.AppStoreActivity;
import com.tianci.appstore.R;
import com.tianci.appstore.controller.AppStoreMainPageController;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyAppDetailData;
import com.tianci.appstore.ui.data.SkyCategoryData;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.mainpage.OnCategoryRefreshListener;
import com.tianci.appstore.ui.mainpage.OnContentRefreshListener;
import com.tianci.appstore.ui.mainpage.OnMainPageControlListener;
import com.tianci.appstore.ui.mainpage.OnPageBoundaryListener;
import com.tianci.appstore.ui.mainpage.SkyAppStoreMainPage;
import com.tianci.appstore.ui.widget.LeftImageButtonLayer;
import com.tianci.appstore.ui.widget.LeftImageButtonLayer.OnLayerFocusChangeListener;
import com.tianci.appstore.ui.widget.SkyAppPager;
import com.tianci.appstore.ui.widget.SkyAppPager.AnimListener;
import com.tianci.appstore.ui.widget.SkyBaseItemView;
import com.tianci.appstore.ui.widget.SkyBaseItemView.OnAppKeyListener;
import com.tianci.appstore.ui.widget.SkyCategoryBar;
import com.tianci.appstore.ui.widget.SkyCategoryBar.CustomFocusCallBack;
import com.tianci.appstore.ui.widget.SkyCategoryBar.OnCategoryChangeListener;
import com.tianci.appstore.ui.widget.SkyCustomLayout;
import com.tianci.appstore.ui.widget.SkyGridAdapter;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;


public class SkyMainPageController extends BaseUIController implements OnAppKeyListener, AnimListener, //OnAppItemClickListener,
        OnCategoryChangeListener, OnCategoryRefreshListener, OnLayerFocusChangeListener,
        OnContentRefreshListener, CustomFocusCallBack, OnPageBoundaryListener {
    private OnPageChangeListener mPageChangeListener = null;
    private OnMainPageControlListener mMainPageControlListener = null;
    private List<SkyCategoryData> tempCgyDatas = null;  // 为了避免在子线程转主线程之间出现mCategoryDatas不同步的问题
    private SkyAppStoreMainPage mMainPage = null;
    private int mKeyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
    private int mTurnPageRow = 0;
    private int mPrePageRow = 0;
    private SkyMyAppController mMyAppController = null;
    private static List<SkyAppBaseData> mManageDatas = null;
    private HashSet<Integer> mPageShowTemp = new HashSet<Integer>();
    private String dataTag = "mytime";
    private int hasRefresh = 0;
    private static SkyCategoryData currentCategory = null;

    public interface OnPageChangeListener {
        public void onPageChange(int currentPage, int totalPage);
    }

    public SkyMainPageController(final Context context) {
        super(context);
        this.mMainPageControlListener = AppStoreMainPageController.getInstance(mContext);
    }

    public static SkyCategoryData getCurrentCategoryData(){
        return currentCategory;
    }

    public void setAppPager(SkyAppPager pager) {
        this.mPager = pager;
        if (AppStoreActivity.defaultOpen)
            mMainPageControlListener.getCategoryData(this);
        else
            mMainPageControlListener.getLocalCategoryData(this);
        mPager.setAnimListener(this);
    }

    public void setMyAppController(SkyMyAppController controller) {
        this.mMyAppController = controller;
    }

    public void setCategoryBar(SkyCategoryBar categoryBar) {
        this.mCategoryBar = categoryBar;
        mCategoryBar.setCustomFocusCallBack(this);
    }

    public void setButtonLayer(LeftImageButtonLayer layer) {
        this.mButtonLayer = layer;
        mButtonLayer.setLayerFocusChangeListener(this);
        mButtonLayer.setVisibility(View.INVISIBLE);
    }

    public void setMainPageLayout(SkyAppStoreMainPage mainPage) {
        this.mMainPage = mainPage;
    }

    public void setPageChangeListener(OnPageChangeListener listener) {
        this.mPageChangeListener = listener;
    }

    private void showNextPage() {
        mCurrentPage++;
        mCategoryBar.setFocusPos(mCurrentPage);
        if (!mCategoryBar.isFocused()) {
            updateTurnPageRow();
        }
        if (mPageShowTemp.contains(mCurrentPage)) {
            mPager.setAnimationDuring(120, 50);
        } else {
            mPageShowTemp.add(mCurrentPage);
            mPager.setAnimationDuring(0, 0);
        }
        mPager.displayNextPage();
        mPager.showNext();
    }

    private void updateTurnPageRow() {
        SkyCustomLayout layout = getCurrentPage();
        int vPos = layout.getBlockFirstPos(mCurrentPos);
        int col = layout.getCols();
        mPrePageRow = layout.getRows();
        mTurnPageRow = vPos / col;
    }

    private void showPrePage() {
        mCurrentPage--;
        mCategoryBar.setFocusPos(mCurrentPage);
        if (!mCategoryBar.isFocused()) {
            updateTurnPageRow();
        }
        if (mPageShowTemp.contains(mCurrentPage)) {
            mPager.setAnimationDuring(120, 50);
        } else {
            mPageShowTemp.add(mCurrentPage);
            mPager.setAnimationDuring(0, 0);
        }
        mPager.displayPrePage();
        mPager.showPrevious();
    }

    public boolean onItemKeyUp(int index, boolean turnPage) {
        LogUtils.d("Controller, OnKeyLeft and the index is " + index);
        if (turnPage && mCurrentPage >= 0) {
            if (mCurrentPage == 0) {
                mCurrentPage = mTotalPage;
            }
            showPrePage();
            return true;
        }
        return false;
    }

    private static final int MSG_REFRESH_CATEGORY = 0;
    private static final int MSG_REFRESH_CONTENT = 1;
    private static final int MSG_REFRESH_CATEGORY_RED = 2;
    private static final int MSG_REFRESH_APPSTATUS = 3;
    private static final int MSG_REFRESH_MYAPP_CORNER = 4;
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_REFRESH_CATEGORY:
                    mCategoryDatas = tempCgyDatas;
                    mCategoryBar.setCategoryDatas(mCategoryDatas);
                    mCategoryBar.clearRedPoint();
                    mTotalPage = mCategoryDatas.size();
                    LogUtils.d(dataTag, "Handle message, refresh category, size = " + mTotalPage);
                    mMainPage.reSizePager(mTotalPage);
                    AppStoreMainPageController.getInstance(mContext).
                            setRefreshMainPageListener(SkyMainPageController.this, mCategoryDatas);
                    if (AppStoreActivity.defaultOpen || (!AppStoreActivity.defaultOpen && mCategoryDatas.size() <= 2))
                        setCategoryFocus();
                    mButtonLayer.setVisibility(View.VISIBLE);
                    currentCategory = mCategoryBar.getCurrentFocusData();
                    break;
                case MSG_REFRESH_CONTENT:
                    LogUtils.d(dataTag, "Handle message, refresh content, cgyId = " + msg.getData().getString("cgyid"));
                    mMainPage.setLoadingVisible(false);
                    int pageIndex = getIndexByCategoryId(msg.getData().getString("cgyid"));
                    if (pageIndex >= mCategoryDatas.size() || pageIndex < 0) {
                        LogUtils.d(dataTag, "    didn't find content page, return");
                        break;
                    }
                    SkyCustomLayout layout = null;
                    if (mCategoryDatas.get(pageIndex).id.equals(AppStoreMainPageController.MINE)) {
                        mMyAppController.refreshContent((List<SkyAppBaseData>) msg.obj);
                        if (mCurrentPage != -1 && mCurrentPage < mCategoryDatas.size() && mCategoryDatas.get(mCurrentPage).id.equals(AppStoreMainPageController.MINE)) {
                            if (!mMyAppController.setFocus()) {
                                mCategoryBar.requestFocus();
                            }
                        }
                        layout = getPage(pageIndex);
                        if (mCategoryDatas != null && mCategoryDatas.size() < 2) {
                            AppStoreMainPageController.myAppLoaded = true;
                            AppStoreMainPageController.getInstance(mContext).getCategoryData(SkyMainPageController.this);
                        }
                    } else {
                        layout = getPage(pageIndex);
                        if (layout != null) {
                            layout.getAdapter().refreshUI((List<SkyAppBaseData>) msg.obj);
                        }
                    }
                    showNoAppTips(layout);
                    long finish = System.currentTimeMillis();
                    LogUtils.d("test", "show content!!");
//                    Toast.makeText(mContext, "从application:"+(finish - CoocaaApplication.startTime)+"\n"+"从activity:"+(finish - AppStoreActivity.activityTime), Toast.LENGTH_LONG).show();
                    break;
                case MSG_REFRESH_CATEGORY_RED:
                    if (msg.obj == null)
                        break;
                    int i = getIndexByCategoryData((SkyCategoryData) msg.obj);
                    if (i != -1) {
                        mCategoryBar.addRedPoint(i, msg.arg1);
                    }
                    break;
                case MSG_REFRESH_APPSTATUS:
                    mMainPage.updateAppStatus(msg.getData().getString("pkgName"), (SkyAppDetailData.AppStatus) msg.obj);
                    break;
                case MSG_REFRESH_MYAPP_CORNER:
                    if (mMyAppController != null)
                        mMyAppController.refreshMyAppCorner((SkyListAppData)msg.obj);
                    break;
                default:
                    break;
            }
        }
    };

    public void setCategoryFocus() {
        int focus = getIndexByCategoryId(AppStoreActivity.mFocusCategoryId);
        if (focus != -1) {
            mCategoryBar.requestFocus();
            mCategoryBar.setFocusPos(focus);
            mPager.setDisplayedChild(focus);
            mCurrentPage = focus;
        }
    }

    ;

    public boolean setFocusToRecommand() {
        if (mCategoryDatas != null && mCategoryDatas.size() > 0) {
            for (int i = 0; i < mCategoryDatas.size(); i++) {
                if (mCategoryDatas.get(i).id.equals("3")) {
                    if (mCurrentPage != i) {
                        if (mCurrentPage < i) {
                            mPager.displayNextPage();
                        } else {
                            mPager.displayPrePage();
                        }
                        mCurrentPage = i;
                        mCategoryBar.setFocusPos(mCurrentPage);
                        mPager.setDisplayedChild(mCurrentPage);
                    } else {
                        return false;
                    }
                    mCategoryBar.requestFocus();
                    return true;
                }
            }
        }
        return false;
    }


    public void onResume() {
        if (mCategoryDatas != null && mCurrentPage > -1 && mCategoryDatas.size() > mCurrentPage
                && mCategoryDatas.get(mCurrentPage).id.equals(AppStoreMainPageController.MINE)) {
            if (mMyAppController != null) {
                if (!mMyAppController.onResume()) {
                    if (mCategoryBar != null && !mButtonLayer.isChildFocused()) {
                        mCategoryBar.requestFocus();
                    }
                }
            }
        }
    }

    public boolean onItemKeyDown(int index, boolean turnPage) {
        LogUtils.d("Controller, OnKeyRight and the index is " + index);
        if (turnPage && mCurrentPage < mTotalPage - 1) {
            showNextPage();
            return true;
        } else if ((turnPage || index == getCurrentPage().getChildCount() - 1) && mTotalPage > 1) {
            if (mCurrentPage == mTotalPage - 1) {
                mCurrentPage = -1;
            }
            showNextPage();
            return true;
        }
        return false;
    }

    public boolean onItemKeyLeft(int index, boolean turnPage) {
        LogUtils.d("Controller, OnKeyLeft and the index is " + index);
        if (turnPage && mCurrentPage > 0) {
            showPrePage();
            return true;
        }
        return false;
    }

    public boolean onItemKeyRight(int index, boolean turnPage) {
        LogUtils.d("Controller, OnKeyRight and the index is " + index);
        if ((turnPage || index == getCurrentPage().getChildCount() - 1) && mCurrentPage < mTotalPage - 1) {
            showNextPage();
            return true;
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, int index, boolean[] turnPage) {
        mCurrentPos = index;
        mKeyCode = keyCode;
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                return onItemKeyLeft(index, turnPage[0]);
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                return onItemKeyRight(index, turnPage[2]);
            case KeyEvent.KEYCODE_DPAD_UP:
//                SkyCustomLayout layout = (SkyCustomLayout) mPager.getChildAt(mPager.getDisplayedChild());
//                if (layout.getBlockFirstPos(mCurrentPos) < layout.getCols())
//                {
//                    mCategoryBar.requestFocus();
//                    return true;
//                }
                break;
            default:
                break;
        }
        return false;
    }

    public void updateUI() {

    }
//
//    @Override
//    public void onClick(int index)
//    {
//        
//    }

    @Override
    public boolean onAnimStart(int current) {
        SkyBaseItemView.mKey = false;
        mCategoryBar.setKey(false);
        return false;
    }

    @Override
    public boolean onAnimEnd(int current) {
        SkyBaseItemView.mKey = true;
        SkyCustomLayout layer = getCurrentPage();
        if (!mCategoryBar.isFocused()) {
            int pos = getNeedFocusPos();
            if (pos == -1) {
                mCategoryBar.requestFocus();
            } else {
                layer.setFocusPos(pos, true);
            }
        }
        showNoAppTips(layer);
        mCategoryBar.setKey(true);
        if (hasRefresh >= mCategoryDatas.size() - 1) {
            if (mKeyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                if (mCurrentPage > 1) {
                    mMainPageControlListener.refreshCagegoryContent(mCategoryDatas.get(mCurrentPage - 1));
                }
            } else if (mKeyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                if (mCurrentPage < mCategoryDatas.size() - 2) {
                    mMainPageControlListener.refreshCagegoryContent(mCategoryDatas.get(mCurrentPage + 1));
                }
            }
        }
        currentCategory = mCategoryBar.getCurrentFocusData();

        return false;
    }

    public void showNoAppTips(SkyCustomLayout layer) {
        if (layer != null && mCategoryDatas != null && mCategoryDatas.size() > 0) {
            if (layer.getChildCount() == 0 && mCategoryDatas.get(mCurrentPage).id.equals(AppStoreMainPageController.MINE)) {
                mMainPage.showTipsView(mContext.getResources().getString(R.string.no_my_app));
            } else {
                mMainPage.hideTipsView();
            }
        }
    }

    private int getNeedFocusPos() {
        SkyCustomLayout layout = getCurrentPage();
        if (layout == null)
            return -1;
        int col = layout.getCols();
        int row = layout.getRows();
        if (mTurnPageRow != 0) {
            mTurnPageRow += row - mPrePageRow;
        }
        row--;
        int temprow = 0;
        if (row < mTurnPageRow) {
            temprow = row;
        } else {
            temprow = mTurnPageRow;
        }
        if (mKeyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            int pos = temprow * col;
            while (pos > 0 && (layout.getBlockViewPos(pos) == -1 || layout.getChildCount() < layout.getBlockViewPos(pos))) {
                pos = pos - col;
            }
            return layout.getBlockViewPos(pos);
        } else if (mKeyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            int pos = (temprow + 1) * col - 1;
            while (pos > 0 && layout.getBlockViewPos(pos) == -1) {
                pos--;
            }
            return layout.getBlockViewPos(pos);
        }
        return 0;
    }

    public static <T> List<List<T>> cutUpList(List<T> dataList, int pageCount) {
        List<T> list = new ArrayList<T>();
        List<List<T>> dataLists = new ArrayList<List<T>>();
        for (int i = 0; i < dataList.size(); i++) {
            if (i == 0 || i % pageCount != 0) {
                list.add(dataList.get(i));
            } else {
                dataLists.add(list);
                list = new ArrayList<T>();
                list.add(dataList.get(i));
            }
            if (i == dataList.size() - 1 && list.size() > 0) {
                dataLists.add(list);
            }
        }
        return dataLists;
    }

    @Override
    public void categoryChanged(SkyCategoryData data, int keyCode) {
        onKeyDown(keyCode, 7, new boolean[]{true, true, true, true});
    }

    @Override
    public void refreshCategory(List<SkyCategoryData> categoryDatas) {
        if (categoryDatas == null) {
            LogUtils.d(dataTag, "Referesh category, categoryDatas == null, return!!!!!!!!!!!");
            return;
        }
        tempCgyDatas = categoryDatas;
        handler.sendEmptyMessage(MSG_REFRESH_CATEGORY);
    }

    @Override
    public void onFocusChange(View leaveView, int leaveIndex, int leaveKey) {
        if (leaveKey == KeyEvent.KEYCODE_DPAD_LEFT) {
            if (mCurrentPage != mTotalPage - 1) {
                showPageByIndex(mTotalPage - 1);
            }
        }
    }

    public void showPageByIndex(int index) {
        mCurrentPage = index;
        mCategoryBar.setFocusPos(mCurrentPage);
        mPager.displayNextPage();
        mPager.setDisplayedChild(mCurrentPage);
    }

    @Override
    public void refreshCategoryRed(SkyCategoryData data, int num) {
        Message msg = handler.obtainMessage(MSG_REFRESH_CATEGORY_RED);
        msg.obj = data;
        msg.arg1 = num;
        handler.sendMessage(msg);
    }

    @Override
    public void refreshScategoryRed(SkyCategoryData categoryData, SkyAppBaseData data, int num) {
        int cagtegoryIndex = getIndexByCategoryData(categoryData);

    }

    @Override
    public void onAppStatusChanged(String pkg, SkyAppDetailData.AppStatus status) {

        Message msg = handler.obtainMessage(MSG_REFRESH_APPSTATUS);

        Bundle bundle = new Bundle();
        bundle.putString("pkgName", pkg);
        msg.setData(bundle);
        msg.obj = status;
        handler.sendMessage(msg);
    }

    @Override
    public void refreshMyAppCorner(SkyListAppData data) {
        Message msg = handler.obtainMessage(MSG_REFRESH_MYAPP_CORNER);
        msg.obj = data;
        handler.sendMessage(msg);
    }

    @Override
    public void refreshContent(List<SkyAppBaseData> appDataLists, SkyCategoryData categoryData) {
        if (categoryData == null) {
            LogUtils.d(dataTag, "Refresh content, categoryData == null, return !!!!!!!!!!!!!");
            return;
        }
        hasRefresh++;
        Message msg = handler.obtainMessage(MSG_REFRESH_CONTENT);
        Bundle data = new Bundle();
        data.putString("cgyid", categoryData.id);
        msg.setData(data);
        msg.obj = appDataLists;
        handler.sendMessage(msg);
        if (categoryData.id.equals(AppStoreMainPageController.GUANLI)) {
            if (appDataLists.size() > 9) {
                mManageDatas = appDataLists.subList(9, appDataLists.size());
            }
        }
    }

    public static List<SkyAppBaseData> getMoreManageData() {
        return mManageDatas;
    }


    @Override
    public boolean setFocus(int keyCode) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            int pos = mCategoryBar.getFocusPos();
            if (mCategoryDatas.size() == 2) {
                switch (pos) {
                    case 0:
                        return setNextFocus(0);
                    case 1:
                        return setNextFocus(0);
                }
            } else if (mCategoryDatas.size() == 3) {
                switch (pos) {
                    case 0:
                        return setNextFocus(0);
                    case 1:
                        return setNextFocus(0);
                    case 2:
                        return setNextFocus(0);
                }
            } else if (mCategoryDatas.size() == 4) {
                switch (pos) {
                    case 0:
                        return setNextFocus(0);
                    case 1:
                        return setNextFocus(3);
                    case 2:
                        return setNextFocus(3);
                    case 3:
                        return setNextFocus(1);
                    default:
                        break;
                }
            } else if (mCategoryDatas.size() == 5) {
                switch (pos) {
                    case 0:
                        return setNextFocus(0);
                    case 1:
                        return setNextFocus(3);
                    case 2:
                        return setNextFocus(3);
                    case 3:
                        return setNextFocus(3);
                    case 4:
                        return setNextFocus(1);
                    default:
                        break;
                }
            } else if (mCategoryDatas.size() == 6) {
                switch (pos) {
                    case 0:
                        return setNextFocus(0);
                    case 1:
                        return setNextFocus(3);
                    case 2:
                        return setNextFocus(3);
                    case 3:
                        return setNextFocus(3);
                    case 4:
                        return setNextFocus(4);
                    case 5:
                        return setNextFocus(1);
                    default:
                        break;
                }
            } else {
                switch (pos) {
                    case 0:
                        return setNextFocus(0);
                    case 1:
                        return setNextFocus(3);
                    case 2:
                        return setNextFocus(3);
                    case 3:
                        return setNextFocus(3);
                    case 4:
                        return setNextFocus(4);
                    case 5:
                        return setNextFocus(4);
                    case 6:
                        return setNextFocus(6);
                    default:
                        break;
                }
            }
        }
        return false;
    }

    private boolean setNextFocus(int pos) {
        SkyCustomLayout layout = getCurrentPage();
        if (layout == null)
            return false;
        if (layout.getChildCount() > pos) {
            layout.setFocusPos(pos, true);
            return true;
        }
        return false;
    }

    @Override
    public boolean pageBoundary(int key, int currentPos) {
        mKeyCode = key;
        mCurrentPos = currentPos;
        if (key == KeyEvent.KEYCODE_DPAD_RIGHT) {
            showNextPage();
            return true;
        }
        return false;
    }

    @Override
    public String getCommand() {
        String json = "";
        try {
            HashMap<String, String[]> command = getGlobalCommand();
            HashMap<String, String[]> fluzz = null;
            if (mCategoryDatas != null && mCategoryDatas.size() > 0) {
                addCategoryCommand(command);
                addItemCommand(command);
                command.put("下载管理", new String[]{"下载管理"});
                command.put("prepage", new String[]{"上一页"});
                command.put("nextpage", new String[]{"下一页"});
            }
            json = JsonUtil.makeScenceJson(
                    getScene(), command, null, null)
                    .toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return json;
    }

    private boolean isMyAppPage() {
        if (mCategoryDatas != null && mCategoryDatas.size() > mCurrentPage) {
            if (mCategoryDatas.get(mCurrentPage).id.equals(AppStoreMainPageController.MINE)) {
                return true;
            }
        }
        return false;
    }

    public void destory(){
        if (mPager != null && mPager.getChildCount() > 0){
            for (int i = 0; i < mPager.getChildCount(); i++){
                SkyCustomLayout layout = getPage(i);
                if (layout != null)
                    layout.getAdapter().destory();
            }
        }
    }

    @Override
    public String getScene() {
        // TODO Auto-generated method stub
        return "com.tianci.appstore:AppStoreActivity";
    }

    @Override
    public boolean exeCommand(Intent intent) {
        if (intent.hasExtra("_scene")
                && intent.getStringExtra("_scene").equals(
                getScene())) {
            if (intent.hasExtra("_command")) {
                String command = intent.getStringExtra("_command");
                LogUtils.d("luwei", "the command is " + command);
                int index = getIndexByCategoryName(command);
                if (index >= 0) {
                    showPageByIndex(index);
                    return true;
                }
                Android.simulateKey(KeyEvent.KEYCODE_BACK, 0);
                if (((SkyGridAdapter) getCurrentPage().getAdapter()).audioMatch(command)) {
                    return true;
                }
                if (command.equals("下载管理")) {
                    mButtonLayer.audioMatch(intent.getStringExtra(command));
                    return true;
                } else if (command.equals("prepage")) {
                    if (isMyAppPage()) {
                        mMyAppController.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, 0, new boolean[]{true, false, false, false});
                    }
                } else if (command.equals("nextpage")) {
                    if (isMyAppPage()) {
                        mMyAppController.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, 3, new boolean[]{false, false, true, false});
                    }
                } else {
                    return super.exeCommand(intent);
                }
            }
        }
        return false;
    }
}
